﻿using System;
using System.Linq;

namespace Microsoft.CSA.FeedSync.FeedSyncLibrarySL
{
    public class Sharing
    {
        private System.Xml.Linq.XElement m_XElement;

        private string m_Since;
        private string m_Until;

        private System.DateTime? m_ExpiresDateTime = null;

        private System.Collections.Generic.List<Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Related> m_RelatedList = new System.Collections.Generic.List<Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Related>();

        public Sharing(System.Xml.Linq.XElement i_SharingXElement)
        {
            if (i_SharingXElement == null)
                throw new System.ArgumentException("i_SharingXElement must be non-null!");

            bool InvalidXElement =
                (i_SharingXElement.Name.LocalName != Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.SHARING_ELEMENT_NAME) ||
                (i_SharingXElement.Name.NamespaceName != Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.FEEDSYNC_XML_NAMESPACE_URI);

            if (InvalidXElement)
                throw new System.ArgumentException("Invalid i_SharingXElement!");

            m_XElement = i_SharingXElement;

            if (m_XElement.Attribute(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.SINCE_ATTRIBUTE) != null)
                m_Since = m_XElement.Attribute(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.SINCE_ATTRIBUTE).Value;

            if (m_XElement.Attribute(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.UNTIL_ATTRIBUTE) != null)
                m_Until = m_XElement.Attribute(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.SINCE_ATTRIBUTE).Value;

            if (m_XElement.Attribute(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.EXPIRES_ATTRIBUTE) != null)
            {
                string Expires = m_XElement.Attribute(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.EXPIRES_ATTRIBUTE).Value;
                m_ExpiresDateTime = System.DateTime.Parse(Expires);
            }

            System.Xml.Linq.XName RelatedXName = Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.FeedSyncXNamespace + Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.RELATED_ELEMENT_NAME;
            var RelatedXElements = i_SharingXElement.Elements(RelatedXName);

            foreach (System.Xml.Linq.XElement RelatedXElement in RelatedXElements)
            {
                Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Related Related = new Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Related
                    (
                    this,
                    RelatedXElement
                    );

                m_RelatedList.Add(Related);
            }
        }

        public Sharing(System.Xml.Linq.XDocument i_XDocument, string i_Since, string i_Until, System.DateTime? i_ExpiresDateTime)
        {
            m_XElement = new System.Xml.Linq.XElement(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.FeedSyncXNamespace + Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.SHARING_ELEMENT_NAME);

            m_Since = i_Since;
            m_Until = i_Until;
            if (i_ExpiresDateTime.HasValue)
                m_ExpiresDateTime = ((System.DateTime)i_ExpiresDateTime).ToUniversalTime();

            if (!System.String.IsNullOrEmpty(m_Since))
                m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.SINCE_ATTRIBUTE, m_Since);

            if (!System.String.IsNullOrEmpty(m_Until))
                m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.UNTIL_ATTRIBUTE, m_Until);

            if (m_ExpiresDateTime != null)
                m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.EXPIRES_ATTRIBUTE, ((System.DateTime)m_ExpiresDateTime).ToString(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.DATE_STRING_FORMAT));
        }

        public System.Xml.Linq.XElement XElement
        {
            get
            {
                return m_XElement;
            }
        }

        public string Since
        {
            get
            {
                return m_Since;
            }
            set
            {
                if (System.String.IsNullOrEmpty(value))
                    throw new System.ArgumentException("Invalid value!");

                m_Since = value;
                m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.SINCE_ATTRIBUTE, value);
            }
        }

        public string Until
        {
            get
            {
                return m_Until;
            }
            set
            {
                if (System.String.IsNullOrEmpty(value))
                    throw new System.ArgumentException("Invalid value!");

                m_Until = value;
                m_XElement.SetAttributeValue(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Constants.UNTIL_ATTRIBUTE, value);
            }
        }

        public System.DateTime? ExpiresDateTime
        {
            get
            {
                return m_ExpiresDateTime;
            }
        }

        public void AddRelated(Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Related i_Related)
        {
            if (m_RelatedList.Contains(i_Related))
                throw new System.ArgumentException("i_Related already added!");

            if (i_Related.XElement.Document != m_XElement.Document)
            {
                System.Xml.Linq.XElement ImportedRelatedXElement = new System.Xml.Linq.XElement(i_Related.XElement);

                i_Related = new Microsoft.CSA.FeedSync.FeedSyncLibrarySL.Related
                    (
                    this,
                    ImportedRelatedXElement
                    );
            }

            m_XElement.Add(i_Related.XElement);

            m_RelatedList.Add(i_Related);
        }
    }
}
